// 验证token的中间件
module.exports = () => {

  // console.log('op', options);

  return async function(ctx, next) {
    let token = ctx.request.headers.authorization;

    // 登录的token： Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwicGFzc3dvcmQiOiJhZG1pbiIsImlhdCI6MTYyMjYyNzcyN30.Sp6degvSWKNGDw77Ylfvz4K0u7mV_1IMZ_Xg94gPrz0

    // 验证的token： eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwicGFzc3dvcmQiOiJhZG1pbiIsImlhdCI6MTYyMjYyNzcyN30.Sp6degvSWKNGDw77Ylfvz4K0u7mV_1IMZ_Xg94gPrz0

    console.log('token', token);
    let decode = '';
    if (token) {
      // 解码token(传过来的是完整的token,但是验证要把Bearer去掉)
      decode = ctx.app.jwt.verify(token.replace("Bearer ",""), ctx.app.config.jwt.secret); // 验证token
      console.log('decode_____', decode);
      await next();
    } else {
      ctx.body = { success: false, info: '没有这个token' };
      return;
    }
  };
};


